using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Admin.NET.Application.AttributeInfo;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Application.Service.Label.Dto;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.NET.Application.Service.Label;

/// <summary>
/// 标签管理
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class LabelService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<LabelManagement> _rep;

	public LabelService(SqlSugarRepository<LabelManagement> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 分页查询标签
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/label/page")]
	public async Task<dynamic> Page([FromQuery] LabelInput input)
	{
		ISugarQueryable<LabelManagement> query = _rep.Context.Queryable<LabelManagement>().WhereIF(!string.IsNullOrEmpty(input.Name), (LabelManagement u) => u.Name.Contains(input.Name)).OrderBuilder(input);
		SqlSugarPagedList<LabelOutPut> list = await query.Select((LabelManagement s) => new LabelOutPut
		{
			Id = s.Id,
			Name = s.Name,
			CreateTime = s.CreateTime,
			Remark = s.Remark
		}).ToPagedListAsync(input.Page, input.PageSize);
		var listMeter = await (from u in _rep.Context.Queryable<ElectricMeter>()
			where u.LabelId != null
			group u by new { u.LabelId } into u
			select new
			{
				LabelId = u.LabelId,
				Count = SqlFunc.AggregateCount(u.LabelId)
			}).ToListAsync();
		List<LabelOutPut> listitem = list.Items.ToList();
		listitem.ForEach(delegate(LabelOutPut u)
		{
			u.Count = (from s in listMeter
				where s.LabelId == u.Id
				select s into u
				select u.Count).FirstOrDefault();
		});
		return list;
	}

	/// <summary>
	/// 增加标签
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/label/add")]
	public async Task Add(LabelInput input)
	{
		LabelManagement entity = input.Adapt<LabelManagement>();
		await _rep.InsertAsync(entity);
	}

	/// <summary>
	/// 删除标签
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/label/delete")]
	public async Task Delete(BaseIdInput input)
	{
		LabelManagement entity = await _rep.GetFirstAsync((LabelManagement u) => u.Id == input.Id);
		if (entity != null)
		{
			if (await (from u in _rep.Context.Queryable<ElectricMeter>()
				where u.LabelId == (long?)entity.Id
				select u).AnyAsync())
			{
				throw Oops.Bah("当前编号已绑定电表");
			}
			await _rep.DeleteAsync(entity);
		}
	}

	/// <summary>
	/// 更新标签
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[OperationLog("更新标签", "name")]
	[HttpPost("/label/edit")]
	public async Task Update(LabelUpdate input)
	{
		LabelManagement entity = input.Adapt<LabelManagement>();
		await _rep.UpdateAsync(entity);
	}

	[HttpGet("/label/getlabeltree")]
	public async Task<dynamic> GetLabelTree()
	{
		return new
		{
			label = "全部",
			id = 0,
			children = (from s in _rep.AsQueryable()
				select new
				{
					label = s.Name,
					id = s.Id
				}).ToList()
		};
	}
}
